﻿using System;
using System.Reflection;

namespace SingleInstanceApplication.ConsoleApp
{
	/// <summary>
	/// 
	/// </summary>
	internal class Program
	{
		/// <summary>
		/// Mains the specified args.
		/// </summary>
		/// <param name="args">The args.</param>
		private static void Main(string[] args)
		{
			if (!ApplicationInstanceManager.CreateSingleInstance(
					Assembly.GetExecutingAssembly().GetName().Name,
			     	SingleInstanceCallback)) return;

			PrintArgs(args);

			Console.Read();
		}

		/// <summary>
		/// Single instance callback handler.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="args">The <see cref="SingleInstanceApplication.InstanceCallbackEventArgs"/> instance containing the event data.</param>
		private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
		{
			if (args == null) return;
			PrintArgs(args.CommandLineArgs);
		}

		/// <summary>
		/// Prints the args.
		/// </summary>
		/// <param name="args">The args.</param>
		private static void PrintArgs(string[] args)
		{
			Console.WriteLine("{0} Received Args:\r\n{1}\r\n=======================\r\n",
			                  DateTime.Now.ToLongTimeString(),
			                  string.Join("\r\n", args));
		}
	}
}